import { ref, reactive } from "vue";
import { ResultEnum } from "@/enums/httpEnum";
import { PORT1 } from "@/api/config/servicePort";
import http from "@/api";
//单行记录interface
interface IOperationRecord {
    action: 'update' | 'insert' | 'delete'; //操作方式
    tableName: string;// 被操作的表名
    fieldName: string;// 修改的字段名
    name?: string;//修改的 子 字段名
    origin?: string;// 修改前的值
    modified?: string;// 修改后的值
    operationTime: string;// 操作时间
}

//UserInfo interface
interface IUserInfo {
    jobId: string;//工号
    name: string;//姓名
    operation: string;//操作
    webUrl: string;//页面
    operationType?: 'insert' | 'delete' | 'update' | 'auditing';//操作方式

}
 interface IResult {
    code: number;
    data: IRecordsData
}
 interface IRecordsData {
    userInfo: IUserInfo;
    baseInfo?: any;
    records: IOperationRecord[];
}

//日志列表数据用于请求参数
interface IGetRecordParams {
    _id: string;// 操作记录id
    user_id: string; // 用户id
    modules: string; // 模块
    action: string; // 操作
    app: string; //
}


interface IUserInfo {
    label: string;
    value: string | number;
}

interface IUserListOptions extends IUserInfo { }


//获取用户列表
export const getUserList = (params = {}) => {
    let userListOptions = reactive<IUserListOptions[]>([]);//用户列表 
    let filterResultOption = ref<IUserListOptions[]>([]);//筛选后结果 
    let selectedValue = ref<string | number>('');
    let filterMethod = (content: string) => { 
        if (!content) { 
            return
        }
        let result = [];
        userListOptions.filter(item => { 
            let reg = new RegExp(content, 'ig');
            if (reg.test(item.label)) {
                result.push(item)
            }
        })

        filterResultOption.value = result;
    }
    //刷新用户列表
    const refreshUserList = (params) => {

        http.get<IUserInfo[]>(PORT1 + "/sys/log/get-user-list", params, { cancel: false, loading: false }).then((res) => {
            if (res.code == ResultEnum.SUCCESS) {

                userListOptions = res.data.map((item) => {
               
                    return {
                        label: item.label,
                        value: item.value
                    }
                })
            }
        })
    }
    // get(params);
    return {
        userListOptions,
        filterResultOption,
        selectedValue,
        filterMethod,
        refreshUserList
    }

}


//获取记录
export const getRecord = (params: IGetRecordParams) => {
    return http.get<IResult>(PORT1 + "/sys/log/log-view", params, { cancel: false, loading: false });

};
 

//获取页面url
 const getRouterParams = (router) => {
    return Promise.resolve(Object.assign({}, router))
}

//页面初始化
export const useSysLogInit = (proxy): void => {
    let recordDetail = reactive<IRecordsData>({});//日志 数据 
    return {
        recordDetail,
        getRecord,
        getUserList,
        getRouterParams
    }

};
// loading
export const useLoading = () => {
    const loadDataStatus = ref<Boolean>(false);
    const setLoadingShow = () => {
        loadDataStatus.value = true;
    }
    const setLoadingHide = () => {
        loadDataStatus.value = false;
    }
    return {
        loadDataStatus,
        setLoadingShow,
        setLoadingHide
    }
};



// 操作方式回调
export const useGetRecordFn = (proxy) => {
    return {
        recordLangFn: {
            insert: (data, isChild) => {
                let category = `log_${data.tableName}`;
                let str = `${proxy.$t("sys_log.insert")} ${data.primary || ""} ${data.fieldName}`;
                if (isChild) {
                    str += `: ${data.name}`;
                }
                str += `: ${data.modified}`;
                return str;
            },
            update: (data, isChild) => {
                let category = `log_${data.tableName}`;
                let str = ``;
                str += data.primary ? `${proxy.$t("sys_log.take")}${data.primary}${proxy.$t("sys_log.of")}` : ``;
                str += ` "${data.fieldName}"`;
                if (isChild) {
                    str += `: ${data.name}`;
                } 
                
                if(data.origin==null||data.origin=='null'||data.origin==''||data.origin=='undefined'||data.origin==undefined){
                    str += ` ${proxy.$t("sys_log.set_to")} “${data.modified}”`;
                }else{
                    str += ` ${proxy.$t("sys_log.from")} “${data.origin}” ${proxy.$t("sys_log.update")} “${data.modified}”`;
                
                }
                
                return str;
            },
            delete: (data, isChild) => {
                let category = `log_${data.tableName}`;
                let str = `${proxy.$t("sys_log.delete")} ${data.primary || ""} ${data.fieldName}`;

                if (isChild) {
                    str += `: ${data.name}`;
                }
                str += ` : ${data.modified}`;
                return str;
            }
        }

    }
}
//显示抽屉 并加载数据
export const useDrawerSysDetail = () => {
    const drawerSysDetail = ref<Boolean>(false)
    const drawerSetShow = (row, getRecordFn) => {
        drawerSysDetail.value = true;
        const params: IGetRecordParams = {
            _id: row._id,
            user_id: row.user_id,
            modules: row.modules,
            action: row.action,
            app: row.app
        };
        getRecordFn(params);
    }

    return {
        drawerSysDetail, drawerSetShow
    }



}